package com.fate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fate.domain.Prod;
import com.fate.domain.ProdProp;
import com.fate.domain.Sku;
import com.fate.service.ProdPropService;
import com.fate.service.ProdService;
import com.fate.service.SkuService;
import com.netflix.discovery.converters.Auto;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("")
public class ProdController {

    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;


    @GetMapping("page")
    @ApiOperation("商品分页查询")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public ResponseEntity<List<Prod>> findProdPage(Page<Prod> page,Prod prod){
       List<Prod> prodPage = (List<Prod>) prodService.page(page,new LambdaQueryWrapper<Prod>()
               .eq(!ObjectUtils.isEmpty(prod.getStatus()),Prod::getStatus,prod.getStatus())
               .like(StringUtils.hasText(prod.getProdName()),Prod::getProdName,prod.getProdName())
               .orderByDesc(Prod::getUpdateTime)
       );
       return ResponseEntity.ok(prodPage);
    }

    /**
     * 上架商品
     *
     */
    @PostMapping
    @ApiOperation("上架商品")
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public ResponseEntity<Void> saveProd(@RequestBody Prod prod){
        prodService.save(prod);
        return ResponseEntity.ok().build();
    }

    //---------------------------------前台接口
    @GetMapping("prod/prodInfo")
    @ApiOperation("根据商品的id查询商品的信息包含sku的集合")
    public ResponseEntity<Prod> getProdInfo(Long prodId){
        Prod prod= prodService.getById(prodId);
        return ResponseEntity.ok(prod);
    }




    /**
     * 提供远程调用根据商品id查询商品对象
     */
    @PostMapping("getProdById")
    @ApiOperation("提供远程调用根据商品id查询商品对象")
    Prod getProdById(@RequestParam Long prodId){
        return prodService.getById(prodId);
    }

    /**
     * 提供远程调用根据skuIds查询sku集合
     */
    @PostMapping("getSkuListByIds")
    @ApiOperation("提供远程调用根据skuIds查询sku集合")
    List<Sku> getSkuListByIds(@RequestBody List<Long> skuIds){
        return (List<Sku>) skuService.listByIds(skuIds);
    }

    /**
     * 根据远程调用修改库存
     */
    @PostMapping("changeStock")
    @ApiOperation("提供远程调用修改库存")
    void changeStock(@RequestBody Map<String, Map<Long,Integer>>map){
        prodService.changeStock(map);
    }
}
